﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Text;

using Continental.Code.BL;
using Continental.Code.Entity;

namespace Continental.Admin
{
    public partial class ManagerNhomTuDiem : System.Web.UI.Page
    {
        public String message;
        public Int32 nhomID = -1;
        public Int32 nhomID1 = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    nhomID = Int32.Parse(Request.QueryString["NhomID"].ToString());
                    ViewState["ID"] = nhomID;
                    ViewState["ACTION"] = "UPDATE";
                    NhomTuDiem theNhomTuDiem = TuDiemProvider.GetNhomTuDiemByID(nhomID);
                    this.txtNumberOfOrder.Text = theNhomTuDiem.NumberOfOrder.ToString();
                    this.txtTitleOrName.Text = theNhomTuDiem.TitleOrName;
                    this.btnSave.Text = "Cập nhật";
                    this.btnDelete.Enabled = true;
                    this.ltlStatus.Text = "Bạn Đang Sửa : " + theNhomTuDiem.TitleOrName;
                    // sử lý thông tin nhóm con
                    LoadNhomTuDiemLevel1(nhomID);
                    try
                    {
                        nhomID1 = Int32.Parse(Request.QueryString["NhomID1"].ToString());
                        ViewState["ID1"] = nhomID1;
                        ViewState["ACTION1"] = "UPDATE";
                        NhomTuDiem theNhomTuDiem1 = TuDiemProvider.GetNhomTuDiemByID(nhomID1);
                        this.txtNumberOfOrder1.Text = theNhomTuDiem1.NumberOfOrder.ToString();
                        this.txtTitleOrName1.Text = theNhomTuDiem1.TitleOrName;
                        this.btnSave1.Text = "Cập nhật";
                        this.btnDelete1.Enabled = true;
                        this.ltlStatus1.Text = "Bạn Đang Sửa : " + theNhomTuDiem1.TitleOrName;
                    }
                    catch
                    {
                        ViewState["ID1"] = null;
                        ViewState["ACTION1"] = "ADD";
                        this.btnSave1.Text = "Thêm mới";
                        this.btnDelete1.Enabled = false;
                        this.ltlStatus1.Text = "Thêm mới";
                    }
                }
                catch
                {
                    ViewState["ID"] = null;
                    ViewState["ACTION"] = "ADD";
                    this.btnSave.Text = "Thêm mới";
                    this.btnDelete.Enabled = false;
                    this.btnDelete1.Enabled = false;
                    this.ltlStatus.Text = "Thêm mới";
                }

                LoadNhomTuDiemLevel0();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            String action = ViewState["ACTION"].ToString();
            Page.Validate("CAPNHAT");
            if (Page.IsValid)
            {
                NhomTuDiem theNhomTuDiem = new NhomTuDiem();
                theNhomTuDiem.TitleOrName = this.txtTitleOrName.Text.Trim();
                theNhomTuDiem.ParentID = 0;
                try
                {
                    theNhomTuDiem.NumberOfOrder = Int32.Parse(this.txtNumberOfOrder.Text.Trim());
                }
                catch
                {
                    theNhomTuDiem.NumberOfOrder = 1;
                }

                if (action.Equals("ADD"))
                {
                    TuDiemProvider.InserNhomTuDiem(theNhomTuDiem);
                    message += "Thêm mới thành công";
                    Response.Redirect("ManagerNhomTuDiem.aspx");
                }
                else if (action.Equals("UPDATE"))
                {
                    theNhomTuDiem.NhomID = Int32.Parse(ViewState["ID"].ToString());
                    TuDiemProvider.UpdateNhomTuDiemByID(theNhomTuDiem);
                    message += "Cập nhật thành công";
                    Response.Redirect("ManagerNhomTuDiem.aspx");
                }
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            nhomID = Int32.Parse(ViewState["ID"].ToString());
            TuDiemProvider.DeleteNhomTuDiemByID(nhomID);
            message += "Xóa thành công";
            Response.Redirect("ManagerNhomTuDiem.aspx");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("ManagerNhomTuDiem.aspx");
        }

        private void LoadNhomTuDiemLevel0()
        {
            DataSet dsResult = TuDiemProvider.GetNhomTuDiemByParentID(0);

            if (dsResult.Tables[0].Rows.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
              
                foreach (DataRow dr in dsResult.Tables[0].Rows)
                {
                    sb.Append("<a href='ManagerNhomTuDiem.aspx?NhomID=" + dr["NhomID"].ToString() + "'>");
                    sb.Append("<H2>" + dr["TitleOrName"].ToString() + "</H2>");
                    sb.Append("</a>");
                }
                this.ltlNhomTuDiemLevel0List.Text = sb.ToString();
            }
        }

        private void LoadNhomTuDiemLevel1(Int32 parentID)
        {
            DataSet dsResult = TuDiemProvider.GetNhomTuDiemByParentID(parentID);

            if (dsResult.Tables[0].Rows.Count > 0)
            {
                StringBuilder sb = new StringBuilder();

                // trường hợp Nhóm lớn có Nhóm con -> không được xóa
                if (dsResult.Tables[0].Rows.Count > 0)
                    this.btnDelete.Enabled = false;

                foreach (DataRow dr in dsResult.Tables[0].Rows)
                {
                    sb.Append("<a href='ManagerNhomTuDiem.aspx?NhomID=" + parentID.ToString() + "&NhomID1=" + dr["NhomID"].ToString() + "'>");
                    sb.Append("<H2>" + dr["TitleOrName"].ToString() + "</H2>");
                    sb.Append("</a>");
                }
                this.ltlNhomTuDiemListLevel1.Text = sb.ToString();
            }
        }

        protected void btnSave1_Click(object sender, EventArgs e)
        {
            nhomID = Int32.Parse(ViewState["ID"].ToString());
            String action = ViewState["ACTION1"].ToString();
            Page.Validate("CAPNHAT1");
            if (Page.IsValid)
            {
                NhomTuDiem theNhomTuDiem = new NhomTuDiem();
                theNhomTuDiem.TitleOrName = this.txtTitleOrName1.Text.Trim();
                theNhomTuDiem.ParentID =Int32.Parse(ViewState["ID"].ToString());
                try
                {
                    theNhomTuDiem.NumberOfOrder = Int32.Parse(this.txtNumberOfOrder1.Text.Trim());
                }
                catch
                {
                    theNhomTuDiem.NumberOfOrder = 1;
                }

                if (action.Equals("ADD"))
                {
                    TuDiemProvider.InserNhomTuDiem(theNhomTuDiem);
                    message += "Thêm mới thành công";
                    Response.Redirect("ManagerNhomTuDiem.aspx?NhomID=" + nhomID);
                }
                else if (action.Equals("UPDATE"))
                {
                    theNhomTuDiem.NhomID = Int32.Parse(ViewState["ID1"].ToString());
                    TuDiemProvider.UpdateNhomTuDiemByID(theNhomTuDiem);
                    message += "Cập nhật thành công";
                    Response.Redirect("ManagerNhomTuDiem.aspx?NhomID=" + nhomID);
                }
            }
        }

        protected void Delete1_Click(object sender, EventArgs e)
        {
            nhomID = Int32.Parse(ViewState["ID"].ToString());
            nhomID1 = Int32.Parse(ViewState["ID1"].ToString());
            TuDiemProvider.DeleteNhomTuDiemByID(nhomID1);
            message += "Xóa thành công";
            Response.Redirect("ManagerNhomTuDiem.aspx?NhomID=" + nhomID);
        }

        protected void btnCancel1_Click(object sender, EventArgs e)
        {
            nhomID = Int32.Parse(ViewState["ID"].ToString());
            Response.Redirect("ManagerNhomTuDiem.aspx?NhomID=" + nhomID);
        }
    }
}
